package com.security.door.controller.areaManage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.crPersionArea;
import com.security.door.model.dto.crPersionAreaDto;
import com.security.door.model.vo.crPersionAreaVo;
import com.security.door.service.IcrPersionAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@Api(value = "WEB - crPersionAreaController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class crPersionAreaController {
    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IcrPersionAreaService icrPersionAreaService;

    @GetMapping("/persionArea/queryListWithPage")
    @ApiOperation(value = "分页查询所有角色信息", notes = "分页查询所有角色信息")
    public Wrapper<PageInfo<crPersionAreaVo>> queryListWithPage() {
        crPersionAreaDto entityDto = new crPersionAreaDto();
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        PageHelper.startPage(pageIndex, pageSize);
        var list = icrPersionAreaService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping("/persionArea/queryListWithPage/{persionCode}")
    @ApiOperation(value = "分页查询所有角色信息", notes = "分页查询所有角色信息")
    public Wrapper<PageInfo<crPersionAreaVo>> queryWithPage(@PathVariable("persionCode") String persionCode) {
        crPersionAreaDto entityDto = new crPersionAreaDto();
        entityDto.setPersionCode(persionCode);
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        PageHelper.startPage(pageIndex, pageSize);
        var list = icrPersionAreaService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping("/persionArea/queryListTree/{persionCode}")
    @ApiOperation(value = "分页查询所有角色信息", notes = "分页查询所有角色信息")
    public Wrapper queryListTree(@PathVariable("persionCode") String persionCode) {
        crPersionAreaDto entityDto = new crPersionAreaDto();
        if (!persionCode.isBlank())
            entityDto.setPersionCode(persionCode);
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        PageHelper.startPage(pageIndex, pageSize);
        var list = icrPersionAreaService.queryListWithPage(entityDto);
        return WrapMapper.ok(this.getTree(list.getList(), null));
    }

    private List<crPersionAreaVo> getTree(List<crPersionAreaVo> list, String parentId) {
        List<crPersionAreaVo> parentNode;
        if (null == parentId)
            parentNode = list.stream().filter(x -> x.getParentcode() == parentId).collect(Collectors.toList());
        else {
            list.removeIf(x -> x.getParentcode() == null);
            parentNode = list.stream().filter(x -> x.getParentcode().equals(parentId)).collect(Collectors.toList());
        }
        List<crPersionAreaVo> listVo = new LinkedList<>();
        parentNode.forEach(x -> {
            crPersionAreaVo temp = new crPersionAreaVo();
            temp.setId(x.getAreacode());
            temp.setText(x.getAreaname());
            temp.setParentcode(parentId);
            List<crPersionAreaVo> tempList = getTree(list, x.getAreacode());
            if (tempList.size() > 0)
                temp.setNodes(tempList);
            listVo.add(temp);
        });
        return listVo;
    }

    @PostMapping("/persionArea/Add")
    @ApiOperation(value = "分页查询所有角色信息", notes = "分页查询所有角色信息")
    public Wrapper Add(@RequestBody List<crPersionArea> list) {
        icrPersionAreaService.deleteByPersionCode(list.get(0).getPersioncode());
        list.forEach(x -> {
            x.setGuid(UUID.randomUUID().toString());
            x.setCreatetime(new Date());
        });
        icrPersionAreaService.batchSave(list);
        return WrapMapper.ok(list);
    }

    @PostMapping("/persionArea/delete")
    @ApiOperation(value = "分页查询所有角色信息", notes = "分页查询所有角色信息")
    public Wrapper Del(@RequestBody crPersionArea entity) {
        icrPersionAreaService.deleteByPersionArea(entity);
        return WrapMapper.ok();
    }
}
